package com.iss.supermarket.EntryUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.MessageWindow.MsgWin;
import com.iss.supermarket.util.entity.Goods;
import com.iss.supermarket.util.network.NetWork;
import com.iss.supermarket.util.repository.GoodsDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class EntryUI extends JFrame {
    //单例模式
    private static EntryUI entryUI;
    //定义组件
    private JLabel Title1,Title2;
    private JLabel[] EntryLabels;
    private JTextField[] EntryTextFields;
    private JButton jButton1;
    private JButton jButton2;
    private JPanel EntryPanel;
    private EntryList entryList;
    private static String BarCode;

    //连接数据库
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    GoodsDao goodsDao = ctx.getBean("GoodsDao", GoodsDao.class);

    public EntryUI(){
        NetWork.getNetWork(EntryUI.class.getSimpleName());
        InitAsembly();
        InitUI();
    }

    private void InitUI(){
        //作为容器使用的组件只在InitUI()中声明
        Box verticalBox1 = Box.createVerticalBox();
        Box verticalBox2 = Box.createVerticalBox();
        Box horizontalBox = Box.createHorizontalBox();
        JPanel rightPanel = new JPanel();
        JPanel leftPanel = new JPanel();

        JPanel titlepanel1 = new JPanel();
        titlepanel1.add(Title1);
        verticalBox1.add(titlepanel1);

        JPanel jPanel1 = new JPanel();
        jPanel1.add(EntryLabels[0]);
        jPanel1.add(EntryTextFields[0]);
        verticalBox1.add(jPanel1);

        JPanel jPanel2 = new JPanel();
        jPanel2.add(EntryLabels[1]);
        jPanel2.add(EntryTextFields[1]);
        verticalBox1.add(jPanel2);

        JPanel jPanel3 = new JPanel();
        jPanel3.add(EntryLabels[2]);
        jPanel3.add(EntryTextFields[2]);
        verticalBox1.add(jPanel3);

        JPanel jPanel4 = new JPanel();
        jPanel4.add(jButton2);
        jPanel4.add(jButton1);
        verticalBox1.add(jPanel4);
        leftPanel.add(verticalBox1);
        horizontalBox.add(leftPanel);

        JPanel titlepanel2 = new JPanel();

        titlepanel2.add(Title2);
        verticalBox2.add(titlepanel2);
        entryList = new EntryList();
        verticalBox2.add(entryList.getPanelPage());
        rightPanel.add(verticalBox2);
        horizontalBox.add(rightPanel);
        EntryPanel.add(horizontalBox);

//        container.add(verticalBox);
//        setSize(800,700);
//        setVisible(true);
    }

    private void InitAsembly(){
        Title1 = new JLabel("入库信息");
        Title1.setFont(new Font("宋体",Font.BOLD, 20));
        Title1.setHorizontalAlignment(SwingConstants.CENTER);
        Title2 = new JLabel("入库列表");
        Title2.setFont(new Font("宋体",Font.BOLD, 20));
        Title2.setHorizontalAlignment(SwingConstants.CENTER);

        EntryLabels = new JLabel[3];
        EntryLabels[0] = new JLabel("条形码：");
        EntryLabels[1] = new JLabel("数量：");
        EntryLabels[2] = new JLabel("商品名称：");
        for(int i=0;i<EntryLabels.length;++i){
            EntryLabels[i].setHorizontalAlignment(SwingConstants.LEFT);
            EntryLabels[i].setPreferredSize(new Dimension(100,30));
            EntryLabels[i].setFont(new Font("黑体",Font.PLAIN,16));
        }

        EntryTextFields = new JTextField[3];
        for(int i=0;i<EntryTextFields.length;++i){
            EntryTextFields[i] = new JTextField();
            EntryTextFields[i].setHorizontalAlignment(SwingConstants.CENTER);
            EntryTextFields[i].setPreferredSize(new Dimension(250, 25));
            EntryTextFields[i].setFont(new Font("黑体",Font.PLAIN,16));
        }

        //设置商品名称不可修改
        EntryTextFields[2].setEnabled(false);

        jButton1 = new JButton("入库");
        jButton1.addActionListener(new ButtonAction());
        jButton1.setFont(new Font("黑体",Font.PLAIN,14));
        jButton2 = new JButton("确定");
        jButton2.addActionListener(new ButtonAction());
        jButton2.setFont(new Font("黑体",Font.PLAIN,14));


        EntryPanel = new JPanel();
        EntryPanel.setPreferredSize(new Dimension(800,700));
    }

    private class ButtonAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            if("确定".equals(e.getActionCommand())){
                //从商品表中查询商品信息，优先存放在入库列表中
                Goods goods = goodsDao.queryGoodsByBarCode(EntryTextFields[0].getText());
                //设置商品入库时的数量
                if("".equals(EntryTextFields[1].getText())){
                    //默认存放一个，扫码时也只存放一个
                    goods.setGdNum(1);
                }else{
                    goods.setGdNum(Integer.parseInt(EntryTextFields[1].getText()));
                }
                //设置商品名称
                EntryTextFields[2].setText(goods.getGdBroadName());
                //刷新入库列表（JList）
                entryList.flushList(goods);
            }else if("入库".equals(e.getActionCommand())){
                //获得入库列表的model
                ListModelArray<Goods> listModelArray = entryList.getListModelArray();

                for(int i=0;i<listModelArray.getSize();++i){
                    //入库uuid是sp_entry的主键，所以每入库listModelArray中的一个元素就重新生成入库uuid
                    String uuid = UUID.randomUUID().toString();
                    Date date = new Date();
                    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String modtime = format.format(date);

                    Goods tmpgoods = listModelArray.getElementAt(i);
                    goodsDao.Entry(uuid,tmpgoods.getGdBarCode(),tmpgoods.getGdNum(),modtime);

                    //入库操作完成，删除前一次的操作数据
                    EntryTextFields[0].setText("");
                    EntryTextFields[1].setText("");
                    EntryTextFields[2].setText("");

//                    System.out.println(tmpgoods.getGdNum());
                }
                //入库完成去除前一次的入库信息
                entryList.Clear();
                MsgWin msgWin = new MsgWin("");
            }
        }
    }

    public static synchronized EntryUI getEntryUI() {
        if(entryUI == null){
            entryUI = new EntryUI();
        }
        return entryUI;
    }
    
    public void setScanningResults(String BarCode){
        //接收扫码结果，更新信息
        EntryTextFields[0].setText(BarCode);
        Goods goods = goodsDao.queryGoodsByBarCode(BarCode);
        EntryTextFields[1].setText("1");
        EntryTextFields[2].setText(goods.getGdBroadName());
    }
    
    public JPanel getEntryPanel(){
        return this.EntryPanel;
    }
}
